<template>
  <div class="search">
    <!-- 自动补全输入框 -->
    <el-autocomplete
      v-model="searchKey"
      :fetch-suggestions="fetchData"
      :trigger-on-focus="false"
      @select="handleSelect"
      placeholder="请输入搜索关键词"
      clearable 
    />
    <!-- 按钮 -->
    <el-button type="primary">搜索</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { reqHospitalInfo } from '@/api/home'
import type { HospitalInfo } from '@/api/home/type'

const $router = useRouter()

const searchKey = ref('')
// 输入获取建议列表
const fetchData = async (keyword: string, cb: Function) => {
  const res: HospitalInfo = await reqHospitalInfo(keyword)
  const fliterData = res.data?.map(item => {
    return {
      value: item.hosname,
      hoscode: item.hoscode
    }
  })
  cb(fliterData)
}

// 选择建议列表进行路由跳转
const handleSelect = (item: any) => {
  // 跳转到医院详情页面
  $router.push({ path: '/hospital-detail', query: { hoscode: item.hoscode } })
}

</script>

<style lang="scss" scoped>
.search {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  ::v-deep(.el-autocomplete) {
    width: 600px;
    margin-right: 10px;
  }
}
</style>
